package com.example.smartcity_poverty.ui.AllService.NumberBook.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.network.AppApi
import com.example.smartcity_poverty.logic.network.ServiceNET.Companion.create
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_number_book_show.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 图书馆详情
 */
class NumberBookShowActivity : AppCompatActivity() {

    // 存储零时照片
    var imageUrl = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_number_book_show)
        title = "图书馆详情"

        // 获取图书馆id
        val id = intent.getStringExtra("id")?.toInt()

        // 填充数据
        id?.let {
            setIdShow(id)
        }

        setEvent()
    }

    private fun setEvent() {

        // 当点击图片的时候放大
        nbShowImage.setOnClickListener {
            glide(imageUrl, isNoImage)
            nbShowBtn.visibility = View.GONE
            isLayout.visibility = View.VISIBLE
        }
        isNoImage.setOnClickListener {
            nbShowBtn.visibility = View.VISIBLE
            isLayout.visibility = View.GONE
        }

    }

    /**
     * 填充数据
     */
    private fun setIdShow(id: Int) {
        GlobalScope.launch(Dispatchers.Main){
            create<AppApi>().getNumberBookAll(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString(),
                id
            ).apply {
                if (this.code == 200){
                    this.data?.let {
                        glide(url + it.imgUrl, nbShowImage)
                        imageUrl = url + it.imgUrl
                        nbShowTitle.text = it.name
                        nbShowThis.text = "地址: ${it.address}"
                        nbShowTime.text = "营业时间: ${it.businessHours}"
                        if (it.businessState.toString() == "1") nbShowIsOk.text = "营业状态: 营业中..."
                        else nbShowIsOk.text = "营业状态: 关门..."
                        nbShowContent.text = it.description

                        val Id = it.id
                        nbShowBtn.setOnClickListener {
                            startActivity(Intent(this@NumberBookShowActivity, ShowUserAllActivity::class.java).putExtra("id", Id.toString()))
                        }
                    }
                }else this.msg?.show()
            }
        }
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}